//
//  ResetPswViewController.m
//  HxgProject
//
//  Created by hxg on 2018/3/30.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "ResetPswViewController.h"
#import "NSString+Extention.h"
#import "LoginService.h"
#import "ResObject.h"
#import <IQKeyboardReturnKeyHandler.h>

@interface ResetPswViewController ()
{
    IQKeyboardReturnKeyHandler *returnKeyHandler;
}

@property (weak, nonatomic) IBOutlet UIButton *renzhengBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneNumField;
@property (weak, nonatomic) IBOutlet UITextField *pswField;

@property (weak, nonatomic) IBOutlet UITextField *veryCodeField;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@end

@implementation ResetPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.backBtn addTarget:self
                     action:@selector(viewMiss:)
           forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view from its nib.
    
    returnKeyHandler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
}

#pragma mark - actions

- (IBAction)getVeryCodeAction:(id)sender {
    if (![NSString valiMobile:self.phoneNumField.text])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    LoginService *loginService = [[LoginService alloc] init];
    [loginService getVerifyCodeByPhone:self.phoneNumField.text withCallBack:^(ResObject *resObj) {
        if (!resObj.isSuccess)
        {
            [SVProgressHUD showErrorWithStatus:@"获取验证码失败!"];
            [SVProgressHUD dismissWithDelay:2.0f];
        }
    }];
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.renzhengBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.renzhengBtn setTitleColor:[UIColor colorWithHexString:@"FB8557"] forState:UIControlStateNormal];
                self.renzhengBtn.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [self.renzhengBtn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                [self.renzhengBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                self.renzhengBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}
- (IBAction)resetAction:(id)sender {
    
    // 先校验
    if (![NSString valiMobile:self.phoneNumField.text])
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    if (self.pswField.text.length < 6 || self.pswField.text.length > 20)
    {
        [SVProgressHUD showErrorWithStatus:@"请检查密码的格式以及位数!"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    if (self.veryCodeField.text.length < 4)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入正确验证码"];
        [SVProgressHUD dismissWithDelay:2.0f];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"加载中..."];
    LoginService *loginService = [[LoginService alloc] init];
    [loginService resetPsw:self.phoneNumField.text
                withnewPwd:self.pswField.text
            withVerifyCode:self.veryCodeField.text
              withCallBack:^(ResObject *resObj) {
                  [SVProgressHUD dismiss];
                  if (resObj.isSuccess) {
                      [SVProgressHUD showSuccessWithStatus:@"重置成功!"];
                      [SVProgressHUD dismissWithDelay:2.0f];
                  } else {
                      [SVProgressHUD showErrorWithStatus:@"重置失败!"];
                      [SVProgressHUD dismissWithDelay:2.0f];
                  }
              }];
}

- (void)viewMiss:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
